package karaoke.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import karaoke.excepcion.DAOExcepcion;
import karaoke.modelo.Local;
import karaoke.util.ConexionBD;

public class LocalDAO extends BaseDAO {

	public Collection<Local> buscarPorNombre(String nombre)
			throws DAOExcepcion {
		System.out.println("LocalDAO: buscarPorNombre(String local_nombre)");
		String query = "select local_Id, local_nombre, local_direccion, local_telefono, local_correo, "
				+ "local_descripcion, local_admin from tb_local where local_nombre like ?";
		Collection<Local> lista = new ArrayList<Local>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, "%" + nombre + "%");
			rs = stmt.executeQuery();
			while (rs.next()) {
				Local vo = new Local();
				vo.setLocal_Id(rs.getInt("local_Id"));
				vo.setLocal_Nombre(rs.getString("local_nombre"));
				vo.setLocal_Descripcion(rs.getString("local_direccion"));
				vo.setLocal_Telefono(rs.getInt("local_telefono"));
				vo.setLocal_Correo(rs.getString("local_correo"));
				vo.setLocal_Descripcion(rs.getString("local_descripcion"));
				vo.setLocal_Admin(rs.getString("local_admin"));
				lista.add(vo);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		System.out.println(lista.size());
		return lista;
	}
	
	
	public Local insertar(Local vo) throws DAOExcepcion {
		String query = "insert into tb_local(local_nombre, local_direccion, local_telefono, local_correo, local_descripcion, local_admin) values (?,?,?,?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getLocal_Nombre());
			stmt.setString(2, vo.getLocal_Direccion());
			stmt.setInt(3, vo.getLocal_Telefono());
			stmt.setString(4, vo.getLocal_Correo());
			stmt.setString(5, vo.getLocal_Descripcion());
			stmt.setString(6, vo.getLocal_Admin());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
			/*
			 * // Obtener el ultimo id int id = 0; query =
			 * "select last_insert_id()"; stmt = con.prepareStatement(query); rs
			 * = stmt.executeQuery(); if (rs.next()) { id = rs.getInt(1); }
			 * vo.setLocal_Id(id);
			 */
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

	public void eliminar(int local_id) throws DAOExcepcion {
		System.out.println("LocalDAO: eliminar(int local_id)");
		String query = "delete from tb_local WHERE local_id=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, local_id);
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo eliminar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

	public Local obtener(int idLocal) throws DAOExcepcion {
		Local vo = new Local();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query = "select local_id, local_nombre, local_direccion, local_telefono, local_correo, local_descripcion, "
					+ "local_admin from tb_local where local_id=?";
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idLocal);
			rs = stmt.executeQuery();
			if (rs.next()) {
				vo.setLocal_Id(rs.getInt(1));
				vo.setLocal_Nombre(rs.getString(2));
				vo.setLocal_Direccion(rs.getString(3));
				vo.setLocal_Telefono(rs.getInt(4));
				vo.setLocal_Correo(rs.getString(5));
				vo.setLocal_Descripcion(rs.getString(6));
				vo.setLocal_Admin(rs.getString(7));
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}
	
	public Collection<Local> listar() throws DAOExcepcion {
		System.out.println("LocalDAO: buscarPorNombre(String nombre)");
		Collection<Local> c = new ArrayList<Local>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "select local_id,local_nombre,local_direccion, local_telefono, local_correo, local_descripcion, "
					+ "local_admin from tb_local order by local_nombre";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Local vo = new Local();
				vo.setLocal_Id(rs.getInt("local_id"));
				vo.setLocal_Nombre(rs.getString("local_nombre"));
				vo.setLocal_Direccion(rs.getString("local_direccion"));
				vo.setLocal_Telefono(rs.getInt("local_telefono"));
				vo.setLocal_Correo(rs.getString("local_correo"));
				vo.setLocal_Descripcion(rs.getString("local_descripcion"));
				vo.setLocal_Admin(rs.getString("local_admin"));
				c.add(vo);
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}

	
	public Local actualizar(Local vo) throws DAOExcepcion {
		String query = "update tb_local  set local_nombre=?, local_direccion=?, local_telefono=?, local_correo=?, local_descripcion=?, local_admin=? where Local_id=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getLocal_Nombre());
			stmt.setString(2, vo.getLocal_Descripcion());
			stmt.setInt(3, vo.getLocal_Telefono());
			stmt.setString(4, vo.getLocal_Correo());
			stmt.setString(5, vo.getLocal_Descripcion());
			stmt.setString(6, vo.getLocal_Admin());
			stmt.setInt(7, vo.getLocal_Id());
			
			
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo actualizar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}
	
}